# coding:utf-8
# python3.6

'''
pdfkit的文档
    https://pypi.python.org/pypi/pdfkit
pdfkit支持的纸张
    http://doc.qt.io/qt-4.8/qprinter.html#PaperSize-enum
BeautifulSoup的文档
    https://www.crummy.com/software/BeautifulSoup/bs4/doc.zh/
PyQt5的文档
    https://pythonspot.com/en/pyqt5-webkit-browser/
'''

import time

import PyQt5
from PyQt5.QtCore import QUrl
from PyQt5.QtWidgets import QApplication, QWidget
from PyQt5.QtWebKitWidgets import QWebView, QWebPage
from PyQt5.QtWebKit import QWebSettings
from PyQt5.QtNetwork import *
from PyQt5.QtCore import QEventLoop
import lxml.html
from bs4 import BeautifulSoup
import pdfkit

URLS = ['https://zhuanlan.zhihu.com/p/27323301',
        'https://zhuanlan.zhihu.com/p/27363298',
        'https://zhuanlan.zhihu.com/p/27391872',
        'https://zhuanlan.zhihu.com/p/27426984',
        'https://zhuanlan.zhihu.com/p/27467127',
        'https://zhuanlan.zhihu.com/p/27484010',
        'https://zhuanlan.zhihu.com/p/27510881',
        'https://zhuanlan.zhihu.com/p/27542160',
        'https://zhuanlan.zhihu.com/p/27667944',
        'https://zhuanlan.zhihu.com/p/27667991',
        'https://zhuanlan.zhihu.com/p/27668010',
        ]

# 打印成pdf文件的选项，用A5是因为比例接近 ipad4 屏幕尺寸便于观看
OPTIONS = {'page-size': 'A5', 
           'encoding': "UTF-8", 
           'custom-header': [ ('Accept-Encoding', 'gzip') ] 
           }

# 下载的html文档没的头，直接打印成pdf会乱码，因此要加个头，注明utf-8编码
html_head = '<head><meta charset="UTF-8"></head>\n'

app = QApplication([])
webview = QWebView()
loop = QEventLoop()


def downpage(url):

    webview.loadFinished.connect(loop.quit)
    webview.load(QUrl(url))
    loop.exec_()
    html = webview.page().mainFrame().toHtml()
    soup = BeautifulSoup(html, 'html.parser')
    title = soup.find(class_="PostIndex-title av-paddingSide av-titleFont")
    content = soup.find(class_="RichText PostIndex-content av-paddingSide av-card")
    
    if title and content:
        file_name = title.contents[0] + '.pdf'
    else:
        file_name = 'no name!'

    page = html_head + str(title) + '\n' + str(content)
    print(file_name)
    return page, file_name

def main():

    while len(URLS) > 0:
        url = URLS.pop()
        [page, file_name] = downpage(url)
        if file_name == 'no name!':
            URLS.append(url)
            continue
        pdfkit.from_string(page, file_name, options=OPTIONS)
        time.sleep(5)

if __name__ == '__main__':
    main()